from peewee import *
db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class UniversityMajorPlanOcr(Model):
    id = PrimaryKeyField()
    page = IntegerField()
    year = IntegerField()
    batch = IntegerField()
    ucode = CharField(max_length=32)
    uname = CharField(max_length=64)
    major_code = CharField(max_length=16)
    remarks = TextField()
    tel = CharField(max_length=128)
    address = CharField(max_length=128)
    type = IntegerField()
    plan_number = IntegerField()
    is_deleted = IntegerField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_major_plan_ocr'  # 如果不指定，默认是person


    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return UniversityMajorPlanOcr.insert(row).execute()
        except Exception as e:
            return ''


    # 批量插入数据
    def toInsertBatch(self, rows):
        with db.atomic():
            UniversityMajorPlanOcr.insert_many(rows).execute()


    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return UniversityMajorPlanOcr.select().where(UniversityMajorPlanOcr.id == id).dicts().get()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getList(self, year):
        try:
            return UniversityMajorPlanOcr.select(
                UniversityMajorPlanOcr.page, UniversityMajorPlanOcr.year, UniversityMajorPlanOcr.batch,
                UniversityMajorPlanOcr.ucode, UniversityMajorPlanOcr.tel, UniversityMajorPlanOcr.address,
                UniversityMajorPlanOcr.uname, UniversityMajorPlanOcr.major_code, UniversityMajorPlanOcr.plan_number,
                UniversityMajorPlanOcr.remarks).where(UniversityMajorPlanOcr.year == year).order_by(UniversityMajorPlanOcr.id.asc()).dicts().execute()
        except Exception as e:
            return ''


    # 根据年份查询列表
    def getListAll(self, year):
        try:
            return UniversityMajorPlanOcr.select().where(UniversityMajorPlanOcr.year == year).order_by(UniversityMajorPlanOcr.id.asc()).dicts().execute()
        except Exception as e:
            print(e)
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getOneByYearBatch(self, year, batch, type, code, major):
        try:
            return UniversityMajorPlanOcr.select().where((UniversityMajorPlanOcr.year == year) &
                                                      (UniversityMajorPlanOcr.batch == batch) &
                                                      (UniversityMajorPlanOcr.ucode == code)).dicts().get()
        except Exception as e:
            return ''


    # 根据年份、批次、文理科类型、院校代码统计记录数
    def getCount(self, year, batch, type, code, major):
        try:
            return UniversityMajorPlanOcr.select().where((UniversityMajorPlanOcr.year == year) &
                                                      (UniversityMajorPlanOcr.batch == batch) &
                                                      (UniversityMajorPlanOcr.ucode == code)).count()
        except Exception as e:
            return ''


    # 更新单条数据(返回影响记录数)
    def toUpdate(self, row, id):
        try:
            return UniversityMajorPlanOcr.update(
                year=row['year'], batch=row['batch'], ucode=row['ucode'], uname=row['uname'],
                major_code=row['major_code'], remarks=row['remarks'], tel=row['tel'],
                address=row['address'], plan_number=row['plan_number']
            ).where(UniversityMajorPlanOcr.id == id).execute()
        except Exception as e:
            return ''    # 根据年份、批次、文理科类型、院校代码统计记录数



if __name__ == '__main__':
    universityMajorPlan = UniversityMajorPlanOcr()
    count = universityMajorPlan.getOneByYearBatch(2022, 9, 1, 10001, '理科试验班类(元培)')